package awd.bj.jls.webs.common;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;

import awd.bj.base.model.Variables;
import awd.bj.jls.model.JbxxModel;
import awd.bj.jls.webs.api.JlsServersApi;
import awd.bj.jls.webs.api.WorkFlowApi;
import awd.bj.jls.webs.api.model.User;
import awd.bj.jls.webs.utils.DefaultQueryParam;
import awd.bj.jls.webs.utils.PagerResult;
import awd.bj.jls.webs.utils.ResponseMessage;
import awd.bj.jls.webs.utils.Result;
import awd.bj.jls.webs.utils.ResultUtils;


@Controller
@RequestMapping("/jls_process")
public class ProcessController {

    @Autowired
    private WorkFlowApi workflowApi;
    @Autowired
    private JlsServersApi jlsServersApi;

    /**
     * 流程待办信息查询
     *
     * @param processDefinitionKey 流程实例id前面的字母部分
     * @param taskDefinitionKey    流程节点ID
     * @param limit                分页显示数量
     * @param start                第几页
     * @return
     */
    @PostMapping("/getProcessTaskList")
    @ResponseBody
    public Map<String, Object> getProcessTaskList(
    		HttpServletRequest request,
            @RequestParam(value = "processDefinitionKey", required = true) String processDefinitionKey,
            @RequestParam(value = "taskDefinitionKey", required = false) String taskDefinitionKey,
            @RequestParam(value = "rows", required = false) String limit,
            @RequestParam(value = "page", required = false) String start
    ) {
        User users = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.err.println("users=="+JSON.toJSONString(users));
        String jsbh = users.getJsbh();
        //Variables variables = new Variables();	//流程查询参数
        Variables variables = DefaultQueryParam.workFlowRequestUtil(request, processDefinitionKey, taskDefinitionKey);
        
        variables.setJsbh(jsbh);

        int _pageIndex = 0;
		String _limit = "10";
		try {
			_pageIndex = Integer.valueOf(start).intValue() - 1;
			_limit = String.valueOf(limit);
		} catch (NumberFormatException e) {
			System.err.println("分页参数出错！");
		}finally {
			variables.setStart(String.valueOf(_pageIndex));
			variables.setLimit(_limit);
		}
        
		System.err.println("variables==="+JSON.toJSONString(variables));
		
        long procedure_startTime = System.currentTimeMillis();   //开始调用接口时间
        
        ResponseMessage<PagerResult<JbxxModel>> result = jlsServersApi.getListCustom(variables);
        Map<String, Object> returnMap = Maps.newHashMap();
        returnMap.put("rows", result.getResult().getData());
        returnMap.put("total", result.getResult().getTotal());
        long procedure_endTime = System.currentTimeMillis(); //获取结束时间
        System.out.println("程序运行时间： " + (procedure_endTime - procedure_startTime) + "ms");
        return returnMap;
    }


    /**
     * 流程代办撤销方法
     *
     * @param processInstanceId
     * @return
     */
    @PostMapping("/Revoke")
    @ResponseBody
    public Result<?> Revoke(@RequestParam(value = "ywlcid") String processInstanceId) {
        String why = "撤销流程!";
        workflowApi.deleteProcessByInstanceId(processInstanceId, why);
        return ResultUtils.success("撤销成功！");
    }


}
